/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.batch.remote.RemoteBatchJobServiceImpl
   Module Description   :

   Date Created      : 2011/3/29
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.batch.remote;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.thread.service.ThreadService;
import com.yaodian100.ec.batch.entity.BatchEvent;
import com.yaodian100.ec.batch.service.BatchEventService;

/**
 * @author jeffma
 * 
 */
public class RemoteBatchJobServiceImpl implements RemoteBatchJobService {

	private final Logger logger = LoggerFactory.getLogger(RemoteBatchJobServiceImpl.class);

	@Autowired
	private BatchEventService service;
	@Autowired
	private ThreadService threadService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.batch.remote.RemoteBatchJobService#checkBatchJob(java.lang.String)
	 */
	@Override
	public boolean checkBatchJob(String tmp) {
		CommonCriteria criteria = new CommonCriteria();
		criteria.addEq("status", BatchEvent.Status.FINISH);
		criteria.addLe("startDate", DateUtil.getCurrentTime());
		List<BatchEvent> events = service.getList(0, -1, criteria, null);
		logger.info("get batch size:{}", events.size());
		for (BatchEvent event : events) {
			try {
				int size = service.addBatch(event.getOid());
				logger.info("add batch to queue, batch oid:{}, size:{}", event.getOid(), size);
			} catch (Exception e) {
				logger.error("add batch fail, batch:" + event, e);
			}
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.batch.remote.RemoteBatchJobService#showInfo()
	 */
	@Override
	public String showInfo() {
		// TODO Auto-generated method stub
		return threadService.showInfo();
	}

}
